package avicit.bdp.lineage.druid.process;

import avicit.bdp.lineage.druid.process.sqlexpr.SQLBaseExprProcessor;
import avicit.bdp.lineage.druid.process.sqlexpr.SQLExprProcessor;
import avicit.bdp.lineage.druid.process.sqlselectquery.SQLSelectQueryProcessor;
import avicit.bdp.lineage.druid.process.statement.StatementProcessor;
import avicit.bdp.lineage.druid.process.tablesource.TableSourceProcessor;
import com.alibaba.druid.sql.ast.SQLExpr;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * Processor注册器 按类型处理
 * </p>
 *
 */
public class ProcessorRegister {

    /**
     * SQLStatement 处理器
     */
    private static final Map<Type, StatementProcessor> STATEMENT_PROCESSOR_MAP = new HashMap<>();

    /**
     * SQLSelectQuery 处理器
     */
    private static final Map<Type, SQLSelectQueryProcessor> SQL_SELECT_QUERY_PROCESSOR_MAP = new HashMap<>();

    /**
     * SQLTableSource 处理器
     */
    private static final Map<Type, TableSourceProcessor> TABLE_SOURCE_PROCESSOR_MAP = new HashMap<>();

    /**
     * SQLTableSource 处理器
     */
    private static final Map<Type, SQLExprProcessor> TABLE_SQL_EXPR_MAP = new HashMap<>();

    private ProcessorRegister() {

    }

    public static void register(Class<?> clazz, Object bean) {
        if (bean instanceof StatementProcessor) {
            STATEMENT_PROCESSOR_MAP.put(clazz, (StatementProcessor) bean);
        } else if (bean instanceof SQLSelectQueryProcessor) {
            SQL_SELECT_QUERY_PROCESSOR_MAP.put(clazz, (SQLSelectQueryProcessor) bean);
        } else if (bean instanceof TableSourceProcessor) {
            TABLE_SOURCE_PROCESSOR_MAP.put(clazz, (TableSourceProcessor) bean);
        } else if (bean instanceof SQLExprProcessor) {
            TABLE_SQL_EXPR_MAP.put(clazz, (SQLExprProcessor) bean);
        }
    }


    public static StatementProcessor getStatementProcessor(Class clazz) {
        StatementProcessor statementProcessor = STATEMENT_PROCESSOR_MAP.get(clazz);
        if (Objects.isNull(statementProcessor)) {
            statementProcessor = STATEMENT_PROCESSOR_MAP.get(clazz.getSuperclass());
        }
        if (Objects.isNull(statementProcessor)) {
            throw new UnsupportedOperationException(clazz.getTypeName());
        }
        return statementProcessor;
    }

    public static SQLSelectQueryProcessor getSQLSelectQueryProcessor(Class clazz) {
        SQLSelectQueryProcessor sqlSelectQueryProcessor = SQL_SELECT_QUERY_PROCESSOR_MAP.get(clazz);
        if (Objects.isNull(sqlSelectQueryProcessor)) {
            sqlSelectQueryProcessor = SQL_SELECT_QUERY_PROCESSOR_MAP.get(clazz.getSuperclass());
        }
        if (Objects.isNull(sqlSelectQueryProcessor)) {
            throw new UnsupportedOperationException(clazz.getTypeName());
        }
        return sqlSelectQueryProcessor;
    }

    public static TableSourceProcessor getTableSourceProcessor(Class clazz) {
        TableSourceProcessor tableSourceProcessor = TABLE_SOURCE_PROCESSOR_MAP.get(clazz);
        if (Objects.isNull(tableSourceProcessor)) {
            tableSourceProcessor = TABLE_SOURCE_PROCESSOR_MAP.get(clazz.getSuperclass());
        }
        if (Objects.isNull(tableSourceProcessor)) {
            throw new UnsupportedOperationException(clazz.getTypeName());
        }
        return tableSourceProcessor;
    }

    public static SQLExprProcessor getSQLExprProcessor(Class clazz) {
        SQLExprProcessor sqlExprProcessor = TABLE_SQL_EXPR_MAP.get(clazz);
        if (Objects.isNull(sqlExprProcessor)) {
            sqlExprProcessor = TABLE_SQL_EXPR_MAP.get(clazz.getSuperclass());
        }
        //父类还是找不到，走默认的处理
        if (Objects.isNull(sqlExprProcessor)) {
            //System.out.println(clazz.getName());
            sqlExprProcessor =  new SQLBaseExprProcessor();
        }
        if (Objects.isNull(sqlExprProcessor)) {
            throw new UnsupportedOperationException(clazz.getTypeName());
        }
        return sqlExprProcessor;
    }

}
